package demo;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * JDBC API 详解 ResultSet
 */
public class JDBCDemo5_ResultSet {

    /**
     * 执行DML语句
     *
     * @throws Exception
     */
    @Test
    public void testDML() throws Exception {
        // 1.注册驱动  (jdk5后此步骤可省略不写)
        // Class.forName("com.mysql.jdbc.Driver");

        // 2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3308/my_java?useSSL=false";
        String username = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, username, password);

        // 3.定义sql
        String sql = "SELECT * FROM account";

        // 4.获取statement对象
        Statement stmt = conn.createStatement();

        // 5.执行sql
        ResultSet rs = stmt.executeQuery(sql);

        // 6.处理结果，遍历rs中所有的数据
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            double money = rs.getDouble("money");

            System.out.println(id);
            System.out.println(name);
            System.out.println(money);
            System.out.println("---------------");

        }

        // 7.释放资源
        rs.close();
        stmt.close();
        conn.close();
    }

}
